const express = require('express');
const path = require('path');

// 创建app实例并设置端口
const app = express();
const port = 8889;

// 挂载静态资源
app.use('/static', express.static(path.join(__dirname, 'static')))

// 自定义中间件控制外部iframe嵌入开关
const checkIframeReferer = require("./utils/iframe-contorl")
app.use(checkIframeReferer);

// 允许跨域请求
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*'); // 允许所有域名
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  next();
});

// 对外暴露html页面接口
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'))
})
app.get('/iframe', (req, res) => {
  res.sendFile(path.join(__dirname, 'iframe.html'))
})

// 导入路由模块
const map_router = require('./router/map')
app.use('/api', map_router)

// 启动服务器
app.listen(port, () => {
  console.log(`Server is running at http://127.0.0.1:${port}`);
  console.log(`Index link: http://127.0.0.1:${port}`);
  console.log(`Link for iframe: http://127.0.0.1:${port}/iframe`);
});
